package com.aaa.service.mht.impl;

import cn.hutool.core.util.ObjectUtil;

import com.aaa.entity.mht.MHTQuestion;
import com.aaa.dao.mht.MHTQuestionDao;
import com.aaa.entity.mht.TestPlanMht;
import com.aaa.entity.system.PageOrder;
import com.aaa.service.mht.MHTQuestionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 问题表(QuestionMht)表服务实现类
 *
 * @author makejava
 * @since 2022-08-30 14:27:41
 */
@Service("questionMhtService")
public class MHTQuestionServiceImpl implements MHTQuestionService {
    @Autowired
    private MHTQuestionDao mhtQuestionDao;

    /**
     *  @create by: 肖成杰
     *  @description: 查询MHT问题表中的所有数据
     *  @create time: 2022/8/30 14:42
     *  @params pageOrder分页排序相关
     *  @params mhtQuestion用于条件查询
     *  @return com.github.pagehelper.Page
     *  */
    @Override
    public Page queryAll(PageOrder pageOrder, MHTQuestion mhtQuestion) {
        //拿出pageOrder里面的所有数据
        PageHelper.startPage(pageOrder.getPage(), pageOrder.getLimit());
        //判断数据是否为空
        if(ObjectUtil.isNotEmpty(pageOrder.getField())){
            PageHelper.orderBy(pageOrder.getFieldOrder());
        }
        //将实体类型转化为Page类
        List<MHTQuestion> list = mhtQuestionDao.queryAll(mhtQuestion);
        Page  pageResult = (Page)list;
        return pageResult;
    }

    /**
     * 根据id删除指定的问题
     * @param ids
     * @return
     */
    @Override
    public boolean delete(int[] ids) {
        return mhtQuestionDao.delete(ids);
    }

    /**
     * 加入一条新的问题
     * @param mhtQuestion
     * @return
     */
    @Override
    public int insertMHT(MHTQuestion mhtQuestion) {
        return mhtQuestionDao.insert(mhtQuestion);
    }

    /**
     * 修改问题
     * @param mhtQuestion
     * @return
     */
    @Override
    public int updateMHT(MHTQuestion mhtQuestion) {
        return mhtQuestionDao.update(mhtQuestion);
    }

    @Override
    public boolean repetitionName(MHTQuestion mhtQuestion) {
        MHTQuestion planMht = mhtQuestionDao.repetitionName(mhtQuestion);
        if(ObjectUtil.isNotEmpty(planMht)){
            Integer mhtId = mhtQuestion.getId();
            if(ObjectUtil.isNotEmpty(mhtId)){
                Integer id = planMht.getId();
                if(mhtId.equals(id)){
                    return true;
                }
            }
            return false;
        }
        return true;
    }

    @Override
    public List<MHTQuestion> queryDictionary() {
        return mhtQuestionDao.queryDictionary();
    }


}
